﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Vortex.Utils.Reflection {

	///<summary>Implements value proxy for field</summary>
	public class PropertyValueProxy : AbstractValueProxy {
		private PropertyInfo _propertyInfo;

		/// <summary>
		/// Initializes a new instance of the <see cref="PropertyValueProxy"/> class.
		/// </summary>
		/// <param name="propertyInfo">The property info.</param>
		/// <param name="instance">The target instance. May be null if property is static.</param>
		public PropertyValueProxy(PropertyInfo propertyInfo, object instance)
			: base(instance) {
			if (null == propertyInfo) throw new ArgumentNullException("propertyInfo");

			_propertyInfo = propertyInfo;
		}

		/// <summary>
		/// Gets the name of property.
		/// </summary>
		/// <value>The name of property.</value>
		public override string Name {
			get { return _propertyInfo.Name; }
		}

		/// <summary>
		/// Gets the type of the value.
		/// </summary>
		/// <value>The type of the value.</value>
		public override Type ValueType {
			get { return _propertyInfo.PropertyType; }
		}

		/// <summary>
		/// Gets or sets the value of target instance.
		/// </summary>
		/// <value>The value of value provider (field, property else).</value>
		public override object Value {
			get { return _propertyInfo.GetValue(Instance, null); }
			set { _propertyInfo.SetValue(Instance, value, null); }
		}
	}
}
